import { defineStore } from 'pinia'
import { $get } from '@/lib/plugin/Aegis'

type RootState = {
  ipInfo: {
    ip: string;
    adcode: string;
    city: string;
    info: string;
    infocode: number;
    province: string;
    rectangle: string;
    status: number
  } | null;
  frontendData: {
    frontend: {
      id: number;
      frontend_login: number;
      frontend_register: number;
      frontend_qq_login: number;
      frontend_github_login: number;
      frontend_email_login: number;
      frontend_comment: number;
      frontend_link: number;
      frontend_dialog: number;
      frontend_dialog_content: string;
      frontend_about: string;
      created_at: string;
      updated_at: string;
      deleted_at: any;
    };
    qq_user: any[];
    article: any;
    comment: any;
  } | null;
  showCatalog: Boolean;
  showPlum: Boolean;
  showMusicAudio: Boolean;
  theme: string;
  CurrentNodeEnv: string;
  full: Boolean;
};

export const appStore = defineStore('app', {
  state: () => {
    return {
      // ip信息
      ipInfo: null,
      // 前端数据
      frontendData: null,
      // 是否显示文章目录
      showCatalog: false,
      // 是否显示梅花特效
      showPlum: false,
      // 是否显示音乐播放器
      showMusicAudio: false,
      // 当前主题
      theme: 'light',
      // 当前node环境
      CurrentNodeEnv: 'development',
      full: true
    } as RootState
  },
  getters: {},
  actions: {
    setIpInfo (res: any): void {
      this.ipInfo = res
    },
    setCurrentNodeEnv (res: any): void {
      this.CurrentNodeEnv = res
    },
    setFrontendData (res: any): void {
      this.frontendData = res
    },
    setShowCatalog (res: any): void {
      this.showCatalog = res
    },
    setShowPlum (res: any): void {
      this.showPlum = res
    },
    setShowMusicAudio (res: any): void {
      this.showMusicAudio = res
    },
    setTheme (res: any): void {
      this.theme = res
      if (res === 'dark') {
        document.body.classList.add('dark')
        document.body.classList.remove('light')
        const styleContent = `
        .dark {
          --bg-color-light: #1a1a1a;
          --text-color-light: #bbb;
          --bg-item-color-light: #1a1a1a;
        }
        `
        const styleEle = document.createElement('style')
        styleEle.textContent = styleContent
        document.head.appendChild(styleEle)
      } else {
        document.body.classList.remove('dark')
        document.body.classList.add('light')
      }
    },
    async getIpInfo () {
      try {
        this.ipInfo = await $get('position/get')
      } catch (error) {
        console.log(error)
      }
    },
    async getFrontendData () {
      try {
        this.frontendData = await $get('frontend/detail')
      } catch (error) {
        console.log(error)
      }
    }
  }
})
